package com.chinanet.utils.pon;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetFinalResult {

	/**
	 * Get TxPower
	 * @param txt
	 * @return
	 */
	public String returnTxPower(String txt) {
		String[] a = getPatternTxPower(txt.toString()).split("\\s+");
		int i=0;
		for(String c:a){
			i++;
			System.out.println(i+"."+c);
		}
		try {
			return a[17];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}
	
	public String returnRxPower(String txt) {
		String[] a = getPatternTxPower(txt.toString().trim()).split("\\s+");
		try {
			return a[15];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}
	
	public String returnSWVER(String txt) {
		String line = "\\--------------------------------------------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			return a[23];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}

	/**
	 *dearUNIinfo
	 * @param txt
	 * @return
	 */
	public String dearUNIinfo(String txt) {
		String line = "\\---------------------------------------------------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			if(a.length<=10){
				return "无数据";
			}else{
				String returnMsg="PORTID  ADMINSTATUS  DUPLEX  SPEED\r\n";
				for(int i=10;i<a.length;i++){
					if(i%10==4||i%10==5||i%10==6)
						returnMsg+=a[i]+"  ";
					if(i%10==7)
						returnMsg+=a[i]+"\r\n";
				}
				return returnMsg;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}

	/**
	 * dear41_Bussinessbits
	 * @param txt
	 * @return
	 */
	public String dear41_Bussinessbits(String txt) {
		String line = "\\-----------------------------------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			if(a.length<=10){
				return "无数据";
			}else{
				String returnMsg="UV（业务流）	SVLAN（外层SVLAN）	CVLAN（内层CVLAN）\r\n";
				String insideMsg="";
				for(int i=10;i<a.length;i++){

					if(i%10==5)
						insideMsg+=a[i]+"			";
					if(i%10==6)
						insideMsg+=a[i]+"\r\n";
					if(i%10==7){
						insideMsg=a[i]+"			"+insideMsg;
						returnMsg+=insideMsg;
						insideMsg="";
					}
				}
				return returnMsg;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}

	public String returnOUNstate(String txt) {
		String line = "\\-----------------------------------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			return a[9];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}

	public String[] returnPtAndIpaddress(String txt) {
		String b[]=new String[2];
		String line = "\\---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			b[0]=a[20];
			b[1]=a[26];
		} catch (Exception e) {
			e.printStackTrace();
			b[0]="无数据";
			b[1]="无数据";
		}
		return b;
	}
	
	public String returnUserNumber(String txt) {
		String line = "\\--------------------------------------------------------------------------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			return a[19];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}
	
	public boolean returnisexist(String txt) {
		String line = "\\-----------------------------------------------------------------------------";
		String[] a = returnMatcher(line, line, txt).trim().split("\\s+");
		int n=7;
		int m=a.length/10;
		boolean flag=false;
//		List<String> UV=new ArrayList<String>();
		try {
			for(int i=0;i<m;i++){
				n=n+10;
				if(n<a.length){
//					UV.add(a[n]);
					if(a[n].trim().equals("41")){
						flag=true;
					}
				}
			}
//			for(String c:UV){
//				System.out.println(c);
//			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	


	/**
	 * block_records num
	 * @param txt
	 * @return
	 */
	public int getPatterns_ONU_block_records(String txt) {
		String returnBack = "";
		String leftpart = "block_records=";
		String rightpart = "LST ONU information";
		returnBack = returnMatcher(leftpart, rightpart, txt);
		returnBack=replaceBlank(returnBack);
		return Integer.parseInt(returnBack);
		
	}
	
	/**
	 * speed
	 * @param txt
	 * @return
	 */
	public String LSTSpeed(String txt){
		String line = "---------------------------------------------------------------";
		return returnMatcher(line, line, txt).trim().split("\\s+")[11];
	}
	
	/**
	 * do Alarm things
	 * @param txt
	 * @return
	 */
	public Map<String,Object> getPatterns_ALARM(String txt){
		Map<String,Object> out=new HashMap<String, Object>();
		txt=getalarmtxt(txt);
		int Alarmfalg=0;
		String DGiTime = "";
		//M
		int DGicount = 0;
		String LOSiTime = "";
		//N
		int LOSicount = 0;
		//PONLos
		int ONTlosflag = 0;
		int DGiTimeflag = 0;
		int LOSiTimeflag = 0;
		//PON口下多个ONT同时掉电
		String[] Pattentext=txt.split("RACK:");
		if(Pattentext.length>0) {
			List<List<String>> firstget = new ArrayList<List<String>>();
			int j = 0;
			for (String string : Pattentext) {
				if (j == 0) {
					List<String> one = new ArrayList<String>();
					String[] a = string.split("\t");
					for (int i = 1; i < a.length; i++)
						one.add(a[i]);
					firstget.add(one);
				} else {
					List<String> other = new ArrayList<String>();
					String[] a = string.split("\t");
					for (String here : a)
						other.add(here);
					firstget.add(other);
				}
				j++;
			}


			for (int i = 0; i < firstget.size() - 1; i++) {
				int end = 11;
				for (int z = 0; z < 11; z++) {
					firstget.get(i).add(firstget.get(i + 1).get(z));
				}

				while (0 < end) {
					firstget.get(i + 1).remove(0);
					end--;
				}
			}
			firstget.remove(firstget.size() - 1);



			for (List<String> list : firstget) {
				if (list.get(0).trim().indexOf("GPON ONT掉电(DGi)") != -1) {
					if (DGicount == 0) {
						DGiTime = list.get(7).trim().substring(0, 16);
//				System.out.println("we"+list.get(7).trim().substring(0,16));
					} else {
						if (DGiTime.equals(list.get(7).trim().substring(0, 16))) {
							DGiTimeflag = 1;
						} else {
							DGiTimeflag = 0;
						}
					}
					DGicount++;
				} else if (list.get(10).trim().indexOf("分支光纤断或OLT检测不到ONT的预期的光信号(LOSi/LOBi)") != -1) {
					if (LOSicount == 0) {
						LOSiTime = list.get(7).trim().substring(0, 16);
//				System.out.println("you"+list.get(7).trim().substring(0,16));
					} else {
						if (LOSiTime.equals(list.get(7).trim().substring(0, 16))) {
							LOSiTimeflag = 1;
						} else {
							LOSiTimeflag = 0;
						}

					}
					LOSicount++;
				} else if (list.get(0).trim().indexOf("PON口下多个ONT同时掉电") != -1) {
					ONTlosflag++;
				}

			}
			//
			for (List<String> list : firstget) {
				int i = 1;
				for (String string : list) {
					System.out.println(i + "." + string);
					i++;
				}

			}
			/**
			 *
			 */
//		System.out.println("M :"+DGicount);
//		System.out.println("MTimeflag :"+DGiTimeflag);
//		System.out.println("N :"+LOSicount);
//		System.out.println("NTimeflag :"+LOSiTimeflag);
//		System.out.println("ONTlosflag"+ONTlosflag);
			if (txt.indexOf("主干光纤断或OLT检测不到预期的光信号（LOS）") != -1) {
				Alarmfalg = 1;
			}
		}
		out.put("Alarmfalg", Alarmfalg);
		out.put("DGinum", DGicount);
		out.put("DGiTimeflag",DGiTimeflag );
		out.put("LOSinum", LOSicount);
		out.put("LOSiTimeflag", LOSiTimeflag);
		out.put("ONTlosflag", ONTlosflag);
		return out;
	}
	
	
	private String getalarmtxt(String txt){
		String returnBack = "";
		String start="PROPOSED_ADVISE";
		String line= "\\------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------";
		returnBack = returnMatcher(start, line, txt);
		return returnBack;
	}
	private String getPatternTxPower(String txt) {
		String returnBack = "";
		String line = "\\---------------------------------------------------------------------------------------------------------------------------------------------------";
		returnBack = returnMatcher(line, line, txt);
		return returnBack;
	}

	private String replaceBlank(String str) {
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	public static String returnMatcher(String leftPart, String rightPart, String txt) {
		String returnBack = "";
		String regex = leftPart + "([\\s\\S].*)" + rightPart;
		Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE
				| Pattern.DOTALL);
		Matcher matcher = pattern.matcher(txt);

		while (matcher.find()) {
			returnBack = matcher.group(1);
		}

		return returnBack;
	}

}
